package org.sdau.rotten.library.controller;

import cn.hutool.core.convert.Convert;
import org.sdau.rotten.library.entity.Book;
import org.sdau.rotten.library.service.BookService;
import org.sdau.rotten.library.dao.BookDAO;
import org.sdau.rotten.library.DTO.JsonData;
import org.sdau.rotten.library.exception.NoBookException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/customer/adminBook")
public class AdminBookController {
    @Autowired
    private BookDAO bookDao;
    @Resource(name = "bookServiceImpl")
    private BookService bookService;

    @ResponseBody
    @GetMapping("/query")
    public JsonData queryBook(@RequestParam("book_id") String bookId) {
        Book book = bookService.getById(Convert.toLong(bookId));

        return JsonData.buildSuccess(book);
    }
    @GetMapping("/addBookPage")
    public String addBookPage() {
        return "add_book";
    }
    @PostMapping("/addBookNum")
    public JsonData AddBookNum(@RequestParam("number") String number, @RequestParam("book_id") String bookId) {

        int back;
        Book book = bookDao.queryById(Convert.toInt(bookId));
        if (book == null) {
            throw new NoBookException("没这本书");
        } else
            back = bookDao.addNumber(Convert.toInt(number), Convert.toInt(bookId));
        if (back > 0) {
            return JsonData.buildSuccess("插入成功");
        } else
            return JsonData.buildError("插入失败");
    }

    @PostMapping("/upload")
    public String saveExcel(@RequestParam("Excel") MultipartFile file) throws IOException {
        Boolean flag = bookService.readExcel(file);
        if(flag) {
            return "redirect:/customer/adminBook/listBook";
        } else {
            return "404";
        }
    }

    @ResponseBody
    @GetMapping("/deleteBook")
    public JsonData DeleteBook(@RequestParam("book_id") String bookId, ModelAndView modelAndView) {
        int back;
        Book book = bookDao.queryById(Convert.toInt(bookId));
        if (book == null) {
            throw new NoBookException("没这本书");
        } else
            back = bookDao.deleteBook(Convert.toInt(bookId));
        if (back > 0) {
            return JsonData.buildSuccess("删除" + bookId + "号书籍成功");
        } else
            return JsonData.buildError("删除" + bookId + "号书籍失败");
    }



    @GetMapping("/listBook")
    public ModelAndView list(ModelAndView model) {
        List<Book> list = bookService.getList();
        model.addObject("list", list);
        model.setViewName("all_book");

        return model;
    }

    @GetMapping("/addBook")
    public String addBook(@RequestParam("book_id") String bookId,
                            @RequestParam("book_name") String bookName,
                            @RequestParam("book_number")String bookNumber) {
        bookService.addBook(Convert.toInt(bookId),bookName,Convert.toInt(bookNumber));

        return "redirect:/listBook";
    }

    @GetMapping("/updateBook")
    public String updateBook(@RequestParam("book_id") String bookId,
                            @RequestParam("book_name") String bookName,
                            @RequestParam("book_number")String bookNumber)
    {
        bookService.updateBook(Convert.toInt(bookId),bookName,Convert.toInt(bookNumber));

        return "redirect:/customer/adminBook/listBook";
    }




}
